package com.pango.system.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pango.core.easyui.json.ResultMessage;
import com.pango.core.easyui.support.UISupportUtils;
import com.pango.core.easyui.vo.ComboTree;
import com.pango.core.easyui.vo.ComboTreeModel;
import com.pango.core.easyui.vo.TreeGrid;
import com.pango.core.easyui.vo.TreeGridModel;
import com.pango.core.utils.StringUtils;
import com.pango.system.entity.OrganizationEntity;
import com.pango.system.model.Organization;
import com.pango.system.services.OrganizationService;

/**
 * 
 * @ClassName: OrganizationController
 * @author thierry.fu
 * @date 2014年7月25日 下午3:27:43
 */

@Controller
@RequestMapping("/organizationController")
public class OrganizationController {
	
	@Autowired
	private OrganizationService organizationService;

	/**
	 * 构建组织机构TreeGrid
	 * @param organization
	 * @param request
	 * @param treegrid
	 * @return
	 */
    @RequestMapping(params = "main")
    @ResponseBody
    public List<TreeGrid> main(Organization organization,HttpServletRequest request, TreeGrid treegrid) {
    	List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
    	List<OrganizationEntity> list = new ArrayList<OrganizationEntity>();
    	//通过上级组织Id进行过滤
    	if(treegrid.getId() == null) {
    		list = this.organizationService.selectOrganizationByParentId(null);
    	} else {
    		list = this.organizationService.selectOrganizationByParentId(Long.parseLong(treegrid.getId()));
    	}
    	TreeGridModel treeGridModel = new TreeGridModel();
		treeGridModel.setTextField("name");
		treeGridModel.setParentText("name");
		treeGridModel.setParentId("parentId");
		treeGridModel.setSrc("available");
		treeGridModel.setIdField("id");
		treeGridModel.setChildList("childList");
        treeGrids = UISupportUtils.treegrid(list, treeGridModel);
        return treeGrids;
    }
    
    @RequestMapping(params = "list")
	public String list() {
		return "org/main";
	}
    
    /**
     * 获取上级部门
     * @param request
     * @param comboTree
     * @return
     */
	@RequestMapping(params = "getParent")
	@ResponseBody
    public List<ComboTree> getParent(HttpServletRequest request, ComboTree comboTree) {
    	List<ComboTree> comboTrees = new ArrayList<ComboTree>();
    	List<OrganizationEntity> list = new ArrayList<OrganizationEntity>();
    	//查找可用的Organization
    	Organization example  = new Organization();
    	if (!StringUtils.isEmpty(request.getParameter("selfId"))) {
    		example.setId(Long.parseLong(request.getParameter("selfId")));
    	} else {
    		example.setId(null);
    	}
    	if(comboTree.getId() == null) {
    		example.setParentId(null);
    	} else {
    		example.setParentId(Long.parseLong(comboTree.getId()));
    	}
    	list = this.organizationService.selectOrganizationNoSelf(example);
    	//end
    	
    	ComboTreeModel comboTreeModel = new ComboTreeModel("id", "name", "childList");
    	
    	comboTrees = UISupportUtils.ComboTree(list, comboTreeModel, null);
    	return comboTrees;
    }
	
	@RequestMapping(params = "add")
	public String add(Organization org, HttpServletRequest req) {
		return "org/organization";
	}
	
	@RequestMapping(params = "update")
	public String update(Organization org, HttpServletRequest req) {
		if(null != org.getId()) {
			OrganizationEntity entity = this.organizationService.getEntityById(org.getId());
			req.setAttribute("org", entity);
		}
		return "org/organization";
	}
	
	@RequestMapping(params = "save")
	@ResponseBody
	public ResultMessage save(OrganizationEntity org, HttpServletRequest req) {
		ResultMessage msg = new ResultMessage();
		this.organizationService.saveEntity(org);
		msg.setMsg("操作成功");
		return msg;
	}
}
